Skip to content

392. Is Subsequence #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

392. Is Subsequence #55

wants to merge 1 commit into from

Conversation

fhiyo
Copy link
Owner

@fhiyo fhiyo commented Aug 17, 2024

def isSubsequence(self, s: str, t: str) -> bool:
i = 0
for c in t:
if i == len(s):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i == len(s) が重複しているのが気になりました。

for c in t:
    if s[i] != c:
        continue
    i += 1
    if i == len(s):
        return True
return False

と書くと良いかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

その方法だとsが空文字のときを別で考慮しないとIndexErrorになるんですよね、ただ見比べるとご提案いただいた方が素直な気がしました。

- 空間計算量: O(n)

```py
class Solution:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この解き方は思いつきませんでした。面白いと思います。

Copy link

@rihib rihib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

色々な解法を試していてとても良いと思いました。僕も参考にさせていただきます

所要時間: 0:47

m: len(s), n: len(t)
- 時間計算量: 分からない。1文字あたりパターンに対して指数時間かかるとすると O(n * 2^m) か?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pythonの正規表現エンジンはバックトラッキングを使用しているようなのでその時間計算量と空間計算量で合ってる気はします(僕も確信はないですが)

https://gihyo.jp/article/2022/11/prevent-vulnerability-0002

def isSubsequence(self, s: str, t: str) -> bool:
i = -1
for c in s:
i = t.find(c, i + 1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

時々標準ライブラリーを見ると発見がありますね。

char_to_indexes[c].append(i)
prev_index = -1
for c in s:
indexes = char_to_indexes[c]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解き方面白いです。
indexesの生存期間が長いので、indexes_of_cくらいの説明があってもいいかもしれませんが(自分は1回目読んでる時最後の方で忘れた)、好みの範囲かもしれません。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexesも使われれますが、indicesという複数形も使われます(元々のラテン語のようです)
matrix -> matrices
index -> indices
などと同じですね。

個人的には教科書などではindicesの方がよく見かける気がしました。
https://stackoverflow.com/questions/1378781/proper-terminology-should-i-say-indexes-or-indices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants